﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace LIMS.DALFactory
{
    internal class DefaultCacheAdapter : ICacheStorage
    {
        private static Dictionary<Type, Type> instances = null;

        private HttpContext context = null;

        public DefaultCacheAdapter()
        {
            instances = new Dictionary<Type, Type>();
            context = HttpContext.Current;
        }

        public  void Remove(String cacheKey)
        {
            context.Cache.Remove(cacheKey);
        }

        public void Add(String key, object data)
        {
            context.Cache.Insert(key, data);
        }

        public T Get<T>(String cachekey)
        {
            var result = (T)context.Cache.Get(cachekey);
            if (result == null)
            {
                result = default(T);
            }
            return result;
        }

        public void Remove(Type type)
        {
            if (instances.ContainsKey(type))
            {
                instances.Remove(type);
            }
        }

        #region MyRegion
        private void Add(Type typeKey, Type typeData)
        {
            if (!instances.ContainsKey(typeKey))
            {
                instances.Add(typeKey, typeData);
            }
        }

       
        private T CreateType<T>()
        {

            if (instances.ContainsKey(typeof(T)))
            {
                return (T)default(T);
            }
            Type typeTemp = instances[typeof(T)];
            T objType = (T)Activator.CreateInstance(typeTemp);
            // Add(typeof(T), typeof(objType));
            return objType;
        } 
        #endregion

    }
}